//authorize.js
const app = getApp()
let baseUrl = app.http.getBaseUrl(); //请求根地址
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        hasAuth: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getUser(res) {
            const info = res.detail
            const ps = getCurrentPages()
            const currentPage = ps[ps.length - 1]
            let sessionKey = wx.getStorageSync('session_key')
            if (info.errMsg === "getUserInfo:ok") {
                wx.setStorage({
                    key: 'user',
                    data: info.userInfo
                })
                this.setData({
                    hasAuth: true,
                })
                currentPage.setData({
                    user: info.userInfo
                })
                app.toPromise(wx.login).then(res => {
                    wx.request({
                        url: baseUrl + 'authorize/info',
                        methods: 'POST',
                        header: {
                            "Encrypted-Data": info.encryptedData,
                            "Iv": info.iv,
                            "Code": res.code,
                            "sid": wx.getStorageSync('sid')
                        }
                    })
                })

            }
        },
    },

    attached() {
        let user = wx.getStorageSync('user')
        app.toPromise(wx.getSetting).then(res => {
            if (res.authSetting['scope.userInfo']) {
                if (user) {
                    this.setData({
                        hasAuth: true,
                    })
                } else {
                    wx.getUserInfo({
                        success: res => {
                            wx.setStorage({
                                key: 'user',
                                data: res.userInfo,
                            })
                            this.setData({
                                hasAuth: true,
                            })
                        }
                    })
                }
            }
        })
    },


})